<?php

App::uses('AppModel', 'Model');

class Product extends AppModel {
    public $name  = 'Product';
    public $order = 'Product.id';
    
    public $belongsTo = array (
        'Group'
    );
    
    public $hasMany = array(
        'ProductImage',
        'ProductDetail' => array(
            'order' => 'ProductDetail.position'
        )
    );
    
    public $validate = array(
        'name' => array(
            'rule'    => 'notEmpty',
            'message' => 'Morate uneti naziv proizvoda'
        )
    );
    
    public function afterDelete() {
        parent::afterDelete();
        
        $path = implode(DS, array('img', 'products', $this->id)); 
        @delete_files($path, array(), true);
    }
    
    public function deleteRelatedData() {
        if ($this->id) {
            $this->ProductImage->deleteAll(array('ProductImage.product_id' => $this->id));
            $this->ProductDetail->deleteAll(array('ProductDetail.product_id' => $this->id));
        }
    }
}

?>